package NK_SEL.pages;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;

//import java.util.concurrent.TimeoutException;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class InternalPage extends AnyPage {

	public InternalPage(PageManager pages) {
		super(pages);
	}
	
  public InternalPage ensurePageLoaded() {
    super.ensurePageLoaded();
    wait.until(presenceOfElementLocated(By.cssSelector("nav")));
    return this;
  }
  
  @FindBy(css = "nav a[href $= '?go=profile']")
  private WebElement userProfileLink;

  @FindBy(css = "nav a[href $= '?go=users']")
  private WebElement userManagementLink;

  @FindBy(css = "nav a[href $= '?logout']")
  private WebElement logoutLink;
  
  @FindBy(css = "nav a[href $= '?go=add']")
  private WebElement filmAddLink;
  
  @FindBy(css = "div.nocover")
  private WebElement filmOpenLink;
  
  @FindBy(css = "img[alt=\"Remove\"]")
  private WebElement filmDeleteLink;
  
  @FindBy(id="q")
  private WebElement filmSearchLink;
 
  public FilmAddPage clickFilmAddPage() {
    filmAddLink.click();
    return pages.filmAddPage;
  }

   public UserProfilePage clickUserProfilePage() {
    userProfileLink.click();
    return pages.userProfilePage;
  }

  public UserManagementPage clickUserManagementLink() {
    userManagementLink.click();
    return pages.userManagementPage;
  }
  
  public LoginPage clickLogoutLink() {
    logoutLink.click();
    wait.until(alertIsPresent()).accept();
    return pages.loginPage;
  }
  
   public InternalPage clickFilmSearch(String s){
	    try   {wait.until(presenceOfElementLocated(By.cssSelector("div.nocover")));
	          } catch (Exception e) {}
		  filmSearchLink.clear();
	  filmSearchLink.sendKeys(s+Keys.RETURN);
	  return this;
  }
  public InternalPage clickfilmOpen() {
	    filmOpenLink.click();
  return this;
  }
  public InternalPage clickfilmDelete() {
	    filmDeleteLink.click();
	    wait.until(alertIsPresent()).accept();
  return this;
  }

  public InternalPage clickfilmNotDelete() {
	    filmDeleteLink.click();
	    wait.until(alertIsPresent()).dismiss();
  return this;
  }
  public InternalPage waitload(Integer nTime){
	  try {	Thread.sleep(nTime);
		} catch (InterruptedException e) {			
		}  
	  return this;
  }
 }

